<?php

namespace app\admin\logic;

use app\common\logic\Base;
use think\facade\Cache;
use think\facade\Config;
use think\facade\App;
use think\facade\Hook;

class AuthGroup extends Base {

    /**
     * @desc 角色列表
     * @param array $params
     * @return array
     */
    public function index($params) {
        $where = [];
        if($params['keyword']) {
            $where[] = ['ag_name', 'like', '%' .$params['keyword']. '%'];
        }
        $field = [
            'ag_id as id',
            'ag_name as name',
            'ag_description as description',
            'ag_rules as rules',
            'ag_status as status',
            'ag_create_time as createTime'
        ];
        $data = App::model('AuthGroup')
            ->where($where)
            ->field($field)
            ->select();

       foreach ($data as $k => $v) {
           $data[$k]['rulesName'] = '';

           //如果管理员展示所有
           if($v['id'] === 1) {
               $name = App::model('AuthRule')->column('ar_name');
               $data[$k]['rulesName'] = implode('，', $name);
               continue;
           }

           if($v['rules']) {
               $name = App::model('AuthRule')
                   ->where('ar_id', 'in', $v['rules'])
                   ->column('ar_name');
               $data[$k]['rulesName'] = implode('，', $name);
           }
       }

        return [
            'list'   => $data,
            'params' => $params
        ];
    }

    /**
     * @desc 设置权限
     * @param array $params
     * @return array
     */
    public function authorize($params) {
        $menu = App::model('AuthRule')->getChildrenTree();

        $rules = App::model('AuthGroup')
            ->where('ag_id', $params['id'])
            ->value('ag_rules');
        $rules = explode(',', $rules);

        return [
            'menu'   => $menu,
            'params' => $params,
            'rules'  => $rules
        ];
    }

    /**
     * @desc 设置权限
     * @param array $params
     * @return array|bool
     */
    public function authorizeAjax($params) {
        if(!$params['menuId']) {
            $this->errorCode = EC_PARAMS_ILLEGAL;
            return false;
        }

        $rules = implode(',', $params['menuId']);
        $result = App::model('AuthGroup')
            ->where(['ag_id' => $params['id']])
            ->update([
                'ag_rules' => $rules
            ]);

        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }

    /**
     * @desc 添加角色
     * @param $params
     * @return array
     */
    public function add($params) {
        return [
            'params'   => $params
        ];
    }

    /**
     * @desc 编辑角色
     * @param $params
     * @return array
     */
    public function edit($params) {
        $list = App::model('AuthGroup')
            ->where(['ag_id' => $params['id']])
            ->find();

        return [
            'info'     => output_format($list, 'ag'),
            'params'   => $params,
        ];
    }

    /**
     * @desc 菜单编辑 (添加、编辑)
     * @param array $params
     * @return array|bool
     */
    public function operateAjax($params) {
        $data = [
            'ag_name'        => $params['name'],
            'ag_description' => $params['description'],
            'ag_status'      => $params['status']
        ];

        if($params['action'] == 'add') {
            $result = App::model('AuthGroup')->insertGetId($data);
        }else {
            $result = App::model('AuthGroup')
                ->where(['ag_id' => $params['id']])
                ->update($data);
        }

        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }
}